package gemini.castor.ui.client.page.content.office.events.edit;

import gemini.basic.model.AbstractDomain;
import gemini.basic.model.Address;
import gemini.basic.model.Event;
import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.ErrorWidget;
import gemini.castor.ui.client.page.widget.address.AddressObject;
import gemini.castor.ui.client.page.widget.address.AddressWidget;
import gemini.castor.ui.client.page.widget.date.DateWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class EventsEditView extends Composite implements
		EventsEditPresenter.Display {

	@UiTemplate("EventsEditView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, EventsEditView> {
	}
	
	private I18nConstants constants;
	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public EventsEditView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
		loadData();
	}

	@UiField
	HTML htmTitle;

	@UiField
	HTML htmInformation;

	@UiField
	DateWidget dwgDate;

	@UiField
	AddressWidget awgAddress;

	@UiField
	TextArea tarDescription;

	@UiField
	Button btnSubmit;

	@UiField
	Button btnBack;

	@UiField
	Button btnReset;
	
	@UiField
	ErrorWidget cnwError;

	@UiHandler("btnReset")
	void resetFields(ClickEvent event) {
		loadForm();
	}

	@Override
	public void initCustom() {
		if (this.event == null) {
			htmTitle.setHTML("<h1>"+ constants.htmNewEvent() +"</h1>");
			htmInformation
					.setHTML(constants.htmNewEventInfo());
		} else {
			htmTitle.setHTML("<h1>"+ constants.htmUpdateEvent() +"</h1>");
			htmInformation.setHTML(constants.htmUpdateEventInfo());
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	private Event event;

	@Override
	public HasClickHandlers getBackClickHandlers() {
		return btnBack;
	}

	@Override
	public HasClickHandlers getSubmitClickHandlers() {
		return btnSubmit;
	}

	@Override
	public void setEvent(Event event) {
		this.event = event;
	}

	@Override
	public void loadForm() {
		cnwError.setVisible(false);
		if (this.event == null) {
			dwgDate.reset();
			awgAddress.reset();
			tarDescription.setValue("");
		} else {
			dwgDate.setDate(this.event.getDate());
			awgAddress.setAddress(this.event.getAddress());
			tarDescription.setValue(this.event.getDescription());
		}
	}

	@Override
	public Event getEvent() {
		if (event == null) {
			event = new Event();
			event.setId(AbstractDomain.TRANSIENT_ENTITY_ID);
		}

		event.setDescription(tarDescription.getValue());
		awgAddress.bind();
		event.setDate(dwgDate.getDateFromDateWidget());
		event.setAddress(createAddressFromAddressObject(event.getAddress()));

		return event;
	}
	
	@Override
	public boolean isValidate(){
		boolean result = awgAddress.isValidate();
		if(!result){
			cnwError.setVisible(true);
		} else {
			cnwError.setVisible(false);
		}
		return result;
	}

	private Address createAddressFromAddressObject(Address add){
		
		if(add == null){
			add = new Address();
		}
		
		add.setAddressLine1(addressObject.getLine1());
		add.setAddressLine2(addressObject.getLine2());
		add.setCity(addressObject.getCity());
		add.setCountry(addressObject.getCountry());
		add.setDistrict(addressObject.getDistrict());
		
		return add;
	}
	
	private AddressObject addressObject;
	
	public void loadData() {
		addressObject = new AddressObject();
		awgAddress.setAddressObject(addressObject);
		cnwError.setMessage(constants.emptyField());
	}
}
